package com.kiragame.xchat.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class XChatDatabaseHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "XChat";
	private static final int DATABASE_VERSION = 4;//update new vs if you edit colunm data

	public static final String TABLE_MESSAGES_NAME = "Messages";
	public static final String TABLE_HISTORY_NAME = "History";

	private static final String DATABASE_MESSAGE_CREATE = "CREATE TABLE "
			+ TABLE_MESSAGES_NAME + "(" + MessagesDataManager.COLUMN_MESSAGE_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
			+ MessagesDataManager.COLUMN_MESSAGE_CREATED_AT + " TEXT, "
			+ MessagesDataManager.COLUMN_MESSAGE_SENDER_ID + " TEXT NOT NULL, "
			+ MessagesDataManager.COLUMN_MESSAGE_CONTENT + " TEXT NOT NULL);";

	private static final String DATABASE_HISTORY_CREATE = "CREATE TABLE "
			+ TABLE_HISTORY_NAME + "("
			+ MessagesDataManager.COLUMN_HISTORY_SENDER_ID
			+ " INTEGER PRIMARY KEY NOT NULL, "
			+ MessagesDataManager.COLUMN_HISTORY_CREATED_AT + " TEXT, "
			+ MessagesDataManager.COLUMN_HISTORY_UNREAD_NUM
			+ " INTEGER NOT NULL, "
			+ MessagesDataManager.COLUMN_HISTORY_IS_FRIEND
			+ " INTEGER NOT NULL, "
			+ MessagesDataManager.COLUMN_HISTORY_LAST_MESSAGE_CONTENT
			+ " TEXT NOT NULL);";

	public XChatDatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_MESSAGE_CREATE);
		db.execSQL(DATABASE_HISTORY_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGES_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY_NAME);
		onCreate(db);
	}

}
